define(['request', 'template', 'jquery', 'cookie'], (request, template) => {
    class Header {
        constructor() {
            this.load().then(() => {
                // 执行加载成功之后的逻辑
                this.search()

                this.getTabs()
                this.calcCartNum()
                this.login()

            })
        }

        // 加载头部html
        load() {
            return new Promise(resolve => {
                // 加载成功之后resolve
                $('header').load('/html/header.html', resolve)
            })

        }
        getTabs() {
            request.getTabs().then(resp => {
                if (resp.code === 200) {
                    let { list } = resp.data
                    list = list.slice(1, 6)
                    $('#blk_header_navbar').html(template('templateList', { list }))
                }
            })
        }

        // 完成搜索功能
        search() {
            $('#inpt_header_search').on('keyup', function() {
                var wd = $('#inpt_header_search').val()
                var url = 'https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=' + wd;
                $.ajax({
                    url: url,
                    dataType: 'jsonp',
                    data: '',
                    jsonp: 'cb',
                    success: function(data) {
                        console.log(data);
                        var str = '';
                        for (var i = 0; i < data.s.length; i++) {
                            str += '<li>' + data.s[i] + '</li>';
                        }
                        $('.show').append(str)
                    },
                    timeout: 3000
                })
                if (wd === '') {
                    $('.show').children().remove()
                }

            })
        }
        calcCartNum() {
            let cartList = localStorage.getItem('cart')
            if (cartList) {
                cartList = JSON.parse(cartList)
                var num = cartList.reduce((total, cart) => {
                    total += cart.num
                    return total;
                }, 0)
                $('#shopNum').html("(" + num + ")")
            } else {
                $('#shopNum').html("(" + 0 + ")")
            }
        }
        login() {
            let user = $.cookie('userinfo')
            console.log(user)
            if (user) {
                $('#unlogin').hide()
                $('#login').show()
                $('#user').html(user)
            } else {
                $('#login').hide()
                $('#unlogin').show()
            }
            $('#logout').on('click', function() {
                $.removeCookie('userinfo')
                $('#login').hide()
                $('#unlogin').show()
            })
        }
        fideIn() {
            $(window).on('scroll', function() {
                clearTimeout(timer)
                var timer = setTimeout(function() {
                    //滚动位置
                    let scrollTop = document.body.scrollTop || document.documentElement.scrollTop;
                    console.log('滚动条位置' + parseInt(scrollTop));
                    if (parseInt(scrollTop) >= 400) {
                        $('header').css('position', 'fixed')
                        $('header').css('z-index', '666')

                    } else {
                        $('header').css('position', '')
                    }

                }, 1000)

            })
        }

    }
    return new Header()
});